﻿using Microsoft.AspNetCore.Mvc;
using System.IO;
using ZhCun.WebUtils;

namespace ZhCun.RightsManage.Controllers
{
    [UserPageAuthorize(false)]
    public class TestController : BasePageController
    {
        public IActionResult GetRootPath()
        {
            return Content(HttpContext.GetWebRootPath());
        }

        public IActionResult GetContentPath()
        {
            return Content(HttpContext.GetContentRootPath());
        }

        /// <summary>
        /// 获取请求跟地址
        /// </summary>
        public string GetRootHost()
        {
            return RemoteIp;
        }

        public string GetSessionId()
        {
            return HttpContext.Session.Id;
        }

        public string SetSession(string val)
        {
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(val);
            HttpContext.Session.Set("tmp", bs);
            return "OK";
        }

        public string GetSession()
        {


            if (HttpContext.Session.TryGetValue("tmp", out byte[] val))
            {
                var str = System.Text.Encoding.UTF8.GetString(val);
                return str;
            }
            else
            {
                return "Fail";
            }
        }

        //public string TestRsa()
        //{
        //    var key = AppCommonConfig.AppSettings.RsaPrivateKey;
        //    var enData = "NN37UXgd6Okl6ilQ17+9zgNQiCBFxxK8mCcs2VqSSgGKdN/Ke0xrNjZY9DOeFdfoXL5eKFx6yrkSoHEMXkWraYicZygyDHBklIkGsz5+p3I+TE5oZxoHRUpn0n9yPUoJUBCKtsAmJeCoaV+B1PPHoz45lsQbQx+CLOJQjOx3+Q==";
        //    //RSAHelper rsa = new RSAHelper(key);
        //    //var r = rsa.Decrypt(enData);
        //    RsaXmlUtil rsa = new RsaXmlUtil(Encoding.UTF8, null, key, 1024);
        //    var r = rsa.Decrypt(enData, RSAEncryptionPadding.Pkcs1);

        //    return r;
        //}

        //public IActionResult SignalRSend1()
        //{
        //    var hb = HttpContext.RequestServices.GetService<IHubContext<MyHub>>();
        //    hb.Clients.All.SendAsync("someFunc", DateTime.Now);
            
        //    return Content("发送成功");
        //}
        
        public IActionResult SignalR()
        {
            return View();
        }

        //[Route("/app/download")]
        //public IActionResult AppDown()
        //{
        //    var rootPath = HttpContext.GetContentRootPath();
        //    string outPath = Path.Combine(rootPath, "appfiles", $"app.apk");
            
        //    using (FileStream fs = new FileStream(outPath, FileMode.Open))
        //    {
        //        byte[] buffer = new byte[fs.Length];
        //        fs.Read(buffer, 0, buffer.Length);               
        //        return File(buffer, "application/octet-stream", $"app.apk");
        //    }
        //}

        //[Route("/app/view")]
        //public IActionResult AppDownView()
        //{
        //    return View();
        //}
    }
}